package chpt03;

import java.time.LocalDate;

public class PersonV4 {
	private int id;
	private String name;
	private String tel;
	private LocalDate date;
	private static int num = 0;

	static { // 静态初始化块：类加载时仅执行一次
		System.out.print("Static initialization block -> ");
		System.out.println("PersonV4.num = " + num);
		//System.out.println("tel = " + tel); // 无法访问非static属性
	}

	{ // 初始化块：每创建一个对象执行一次
		System.out.print("Initialization block -> ");
		num++;
		id = num;
		System.out.println("num = " + num + " id = " + id);
	}

	public PersonV4(String name) {
		this.name = name;
	}

	public PersonV4(String name, String tel) {
		this.name = name;
		this.tel = tel;
	}

	public PersonV4(String name, String tel, int y, int m, int d) {
		this.name = name;
		this.tel = tel;
		this.date = LocalDate.of(y, m, d);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public LocalDate getDate() {
		return date;
	}

	public void setDate(LocalDate date) {
		this.date = date;
	}

	@Override
	public String toString() { // 不出现类属性num
		return "PersonV4 [id=" + id + ", name=" + name + ", tel=" + tel + ", date=" + date + "]";
	}

	public static void showNum() {
		System.out.println("static方法 -> PersonV4.showNum = " + num);
		// System.out.println("tel = " + tel); // 无法访问非static属性
	}

	// getter/setter代码
	public static void main(String[] args) {
		// 所有构造方法均不需设置id
		PersonV4 p1 = new PersonV4("Tom");
		PersonV4 p2 = new PersonV4("Jerry", "133***");
		PersonV4 p3 = new PersonV4("Java", "188***", 2010, 6, 1);
		System.out.println(p1);
		System.out.println(p2);
		System.out.println(p3);
		// 推荐：通过"类名.类属性名"访问类属性
		System.out.println("PersonV4.num = " + PersonV4.num);
		// 也可通过“对象变量.类属性名”访问类属性，不推荐
		System.out.println(p1.num); // p1、p2、p3三个对象共享一个num
		System.out.println(p2.num);
		System.out.println(p3.num);
		PersonV4.showNum(); // 调用静态方法
	}
}
